import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.config.productionTip = false
Vue.use(ElementUI)

Vue.use(VueAxios, axios)

// 全局配置
Vue.axios.defaults.baseURL = 'http://8.217.172.199:8082'
// Vue.axios.defaults.baseURL = 'http://127.0.0.1:8082'
Vue.axios.defaults.timeout = 120000

// 请求拦截器
Vue.axios.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token')
        if (token) {
            config.headers.Authorization = `Bearer ${token}`
        }
        return config
    },
    error => {
        console.error('请求错误:', error)
        return Promise.reject(error)
    }
)

// 响应拦截器
Vue.axios.interceptors.response.use(
    response => {
        return response.data
    },
    error => {
        console.error('响应错误:', error)
        if (error.response) {
            switch (error.response.status) {
                case 401:
                    // 处理未授权
                    break
                case 404:
                    // 处理资源不存在
                    break
                case 500:
                    // 处理服务器错误
                    break
            }
        }
        return Promise.reject(error)
    }
)


new Vue({
    router,
    render: h => h(App)
}).$mount('#app')

